package com.mall.service.impl;

import com.mall.dao.IOrderDao;
import com.mall.domain.Order;
import com.mall.domain.Page;
import com.mall.service.IOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单的业务层接口实现类
 * @author : liangY
 * @date : 2021-01-12 23:30
 */
@Service("orderService")
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private IOrderDao orderDao;

    /**
     * 查询所有订单
     *
     * @param page 分页实体类
     * @return
     */
    @Override
    public List<Order> findAll(Page page){
        //判断是否为模糊查询
        if(page.getQuery() == null){
            page.setQuery("%");
        } else {
            page.setQuery("%" + page.getQuery() + "%");
        }
        //设置分页查询开始值
        page.setNum((page.getNum() - 1) * page.getSize());
        return orderDao.findAll(page);
    }

    /**
     * 查询所有订单数量
     *
     * @return
     */
    @Override
    public Integer findTotal(){
        return orderDao.findTotal();
    }

    /**
     * 更新订单
     *
     * @param order 订单实体类
     * @return
     */
    @Override
    public Integer update(Order order){
        order.setUpdated(new Date());
        if(order.getStatus() == 3){
            order.setConsign_time(new Date());
        }
        return orderDao.update(order);
    }

    /**
     * 添加订单
     *
     * @param order 订单实体类
     * @return
     */
    @Override
    public Integer add(Order order){
        order.setCreated(new Date());
        return orderDao.add(order);
    }

    /**
     * 删除订单
     *
     * @param id
     * @return
     */
    @Override
    public Integer del(Integer id){
        return orderDao.del(id);
    }
}
